// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import api from './config/api'
import util from './config/util'
import VueLazyload from 'vue-lazyload'
import LyTab from "ly-tab";
require('./common/fastclick.js');
require('es6-promise').polyfill();

Vue.use(LyTab);

if ('addEventListener' in document) {
  document.addEventListener('DOMContentLoaded', function() {
    FastClick.attach(document.body);
  }, false);
}
// 将方法绑定到全局
Vue.prototype.$api = api
Vue.prototype.$util = util

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App },
  mounted(){
    let docWidth = 750;
    let doc = window.document,
      docEl = doc.documentElement,
      resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize';

      let recalc = (function refreshRem () {
      let clientWidth = docEl.getBoundingClientRect().width;
      /* 8.55：小于320px不再缩小，11.2：大于420px不再放大 */
      //docEl.style.fontSize = Math.max(Math.min(20 * (clientWidth / docWidth), 11.2), 8.55) * 5 + 'px';
      docEl.style.fontSize = Math.max(20 * (clientWidth / docWidth), 8.55) * 5 + 'px';
      return refreshRem;
    })();

    docEl.setAttribute('data-dpr', window.navigator.appVersion.match(/iphone/gi) ? window.devicePixelRatio : 1);
    if (/iP(hone|od|ad)/.test(window.navigator.userAgent)) {
      doc.documentElement.classList.add('ios');
    }
    if (!doc.addEventListener) return;
    window.addEventListener(resizeEvt, recalc, false);
    doc.addEventListener('DOMContentLoaded', recalc, false);
  }
})

Vue.use(VueLazyload, {
  preLoad: 1.3,
  // error: 'dist/error.png',
  loading: require('./assets/loading.gif'),
  attempt: 1
});

Vue.directive('title', {
  inserted: function (el, binding) {
    document.title = el.dataset.title
  }
})
